package com.softstao.guoyu.global;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.softstao.guoyu.R;
import com.softstao.guoyu.model.Payload;
import com.softstao.guoyu.ui.activity.MainActivity;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xuhon on 2016/8/25.
 */
public class GYApplication extends MultiDexApplication {
    public static GYApplication application;
    private static DemoHandler handler;
    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        CrashReport.initCrashReport(getApplicationContext(), "6a22dfbd0c", true);
        
        PlatformConfig.setWeixin(SharePreferenceManager.WX_APPID, SharePreferenceManager.WX_APPKEY);
        MobclickAgent.startWithConfigure(new MobclickAgent.UMAnalyticsConfig(this, "595da08d2ae85b256800148c", "guoyu", MobclickAgent.EScenarioType. E_UM_NORMAL,true));
        UMShareAPI.get(this);
        PushAgent.getInstance(this).setAppkeyAndSecret("595da08d2ae85b256800148c","9a4c9e7ba3c490692b6f1175e75daafc");
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();

        UserManager.getInstance().setUser(SharePreferenceManager.getInstance().getUser());

        OkHttpUtils.getInstance()
                .init(this)
                .debug(true, "okHttp")
                .timeout(20 * 1000);

        if (handler == null) {
            handler = new DemoHandler();
        }
    }

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    public class DemoHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Payload mPayload = new Payload();
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        mPayload.setId(jsonObject.optInt("id"));
                        mPayload.setTitle(jsonObject.optString("title"));
                        mPayload.setContent(jsonObject.optString("content"));
                        String redirect  = TextUtils.isEmpty(jsonObject.optString("redirect"))?"":jsonObject.optString("redirect");
                        mPayload.setRedirect(redirect);
                        notice(mPayload);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    private void notice(Payload payload){
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder mBuilder = new Notification.Builder(this);
        mBuilder.setContentTitle(payload.getTitle())
                .setContentText(payload.getContent())
                .setContentIntent(getDefalutIntent())
                .setTicker("果语消息")
                .setWhen(System.currentTimeMillis())
                .setOngoing(false)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setSmallIcon(R.mipmap.logo);
        mNotificationManager.notify(payload.getId(),mBuilder.build());
    }

    public PendingIntent getDefalutIntent(){
        PendingIntent pendingIntent= PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
        return pendingIntent;
    }
}
